package service

import (
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"user-center/src/common"
	"user-center/src/constant"
	"user-center/src/db"
	"user-center/src/model"
	"user-center/src/model/request"
	"user-center/src/model/response"
)

func TaskAdd(ctx *gin.Context, taskRequest *request.TaskAddRequest, user *model.User) *response.BaseResponse {
	res := &response.BaseResponse{}
	if taskRequest == nil {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		return res
	}
	if user == nil {
		res.ResponseWithError(common.NOT_LOGIN, "未登录")
		return res
	}

	// 1 检查groupId [1 有效 , 2 todo 查看是否有这个群]
	_, err := primitive.ObjectIDFromHex(taskRequest.GroupId)
	if err != nil {
		res.ResponseWithError(common.PARAMS_ERROR, "InvalId chat group Id")
		return res
	}
	// 2 title 唯一
	var count int64
	if err = db.GetMySQL().Table("task").Where("title = ? and isDelete = 0", taskRequest.Title).Count(&count).Error; err != nil {
		res.ResponseWithError(common.SYSTEM_ERROR, "数据库错误")
		return res
	}
	if count != 0 {
		res.ResponseWithError(common.PARAMS_ERROR, "任务已经存在")
		return res
	}

	// 3 检查userId
	var userId []int64
	for _, i := range taskRequest.MemberTasks {
		userId = append(userId, i.UserId)
	}

	var existsUserId []int64
	if err = db.GetMySQL().Table("user").Select("id").Where("id in (?) ", userId).Find(&existsUserId).Error; err != nil {
		res.ResponseWithError(common.SYSTEM_ERROR, "数据库错误")
		return res
	}

	var taskList []*model.Task
	for _, i := range taskRequest.MemberTasks {
		for _, j := range existsUserId {
			if i.UserId == j {
				task := &model.Task{
					GroupId:      taskRequest.GroupId,
					CreateUserId: user.Id,
					UserId:       i.UserId,
					Title:        taskRequest.Title,
					Description:  i.Task,
					Priority:     taskRequest.Priority,
					Status:       constant.COMMISSION,
					ExpireTime:   taskRequest.ExpireTime,
				}
				taskList = append(taskList, task)
			}
		}
	}

	if err = db.GetMySQL().Select("groupId", "createUserId", "userId", "title", "description", "priority", "status", "expireTime").Create(&taskList).Error; err != nil {
		res.ResponseWithError(common.SYSTEM_ERROR, "数据库错误")
		return res
	}
	// 返回Id
	return res
}

func TaskAll(ctx *gin.Context, groupId string) *response.BaseResponse {
	res := &response.BaseResponse{}

	var task []model.Task
	if err := db.GetMySQL().Where("isDelete = 0 and groupId = ?", groupId).Find(&task).Error; err != nil {
		res.ResponseWithError(common.SYSTEM_ERROR, "数据库查询错误")
		return res
	}
	res.ResponseWithData(task)
	return res
}

func TaskInfo(ctx *gin.Context, groupId string) *response.BaseResponse {
	res := &response.BaseResponse{}
	return res
}

func TaskByUser(ctx *gin.Context, groupId string, user *model.User) *response.BaseResponse {
	res := &response.BaseResponse{}
	if user == nil {
		res.ResponseWithError(common.NOT_LOGIN, "未登录")
		return res
	}

	var task []model.Task
	if err := db.GetMySQL().Where("isDelete = 0 and groupId = ? and userId = ? ", groupId, user.Id).Find(&task).Error; err != nil {
		res.ResponseWithError(common.SYSTEM_ERROR, "数据库查询错误")
		return res
	}
	res.ResponseWithData(task)
	return res
}

func TaskChange(ctx *gin.Context, req *request.TaskChangeRequest, user *model.User) *response.BaseResponse {
	res := &response.BaseResponse{}
	if req == nil {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		return res
	}
	if user == nil {
		res.ResponseWithError(common.NOT_LOGIN, "未登录")
		return res
	}

	if err := db.GetMySQL().Table("task").Where("status = ? - 1 and id = ?", req.Status, req.Id).Update("status", req.Status).Error; err != nil {
		res.ResponseWithError(common.SYSTEM_ERROR, "修改失败")
		return res
	}

	return res
}
